/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Free Documentation License
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of this
** file.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
    \title Qt Bubble Level Example
    \example

    Qt Bubble Level is a simple application that uses accelerometer sensor information to calculate the inclination of the device 
    and presents this as a traditional Bubble level. The application provides a calibration feature to handle any possible errors in accelerometer readings. The example is hosted in Projects Forum Nokia: https://projects.forum.nokia.com/qtbubblelevel.
    
    \image qml-qtbubblelevel-example.png
    
    \section1 Initialising the application
    
    All of the initialisations are done in the main function. 
    
    First the QDeclarativeView is created to intepret the QML files. The QML file given is found from Qt resource. The root QML element is set to resize to the view whenever the view is resized.

    \snippet main.cpp 0
    
    The Settings object will handle the loading / storing of the calibration value. Then we create a instances from QAccelerometer and AccelerometerFilter and attach the filter into the sensor.
    
    \snippet main.cpp 1

    The Qt code is then connected to QML code by using Qt Signals and Slots connections. First the rootobject is retrieved QDeclarativeEngine which is retrieved from QDeclarativeView. The root object represents now the Qt object of the QML's root element.
    
    The saveCorrectionAngle signal of the QML root element is connected to Qt slot saveCorrectionAngle. The rotationChanged and correctionAngle Qt signals are connected to handleRotation and setCorrectionAngle slot of QML root element. Finally the quit signal of the QDeclarativeEngine is connected to QApplication's quit slot.
    
    \snippet main.cpp 2
    
    On the Maemo target the application needs an minimize button so we connect one additional QML signal to Qt slot. The minimize button is made visible by setting the QML root element's taskSwitcherVisible property to value true.
    
    \snippet main.cpp 3
    
    The correction factor of the accelerometer is retrieved from persistent storage by using QSettings. The correction factor is signalled to QML side by using function setCorrectionAngle. The accelerometer sensor is started and it will eventually begin to signal the changes in accelerometer readings.
    
    \snippet main.cpp 4
    
    Finally, in the end of the function the view is shown in full screen in mobile devices. In other targets the application is shown as 800 x 480 resolution on the 100, 100 position from the top-left corner of the desktop.
    
    \snippet main.cpp 5
    
    \section1 Accessing the accelerometer information
    
    The inclination of the device is resolved by using QAccelerometer sensor of the QtMobility. We already created the sensor in the main function and attached our self derived AccelerometerFilter object to it. Here is the definition of the AccelerometerFilter class:
    
    \snippet accelerometerfilter.h 0
    
    The class is multiderived from QObject and QAccelerometerFilter classes. The QAccelerometerFilter class is derived from QObject because we want to use Qt Signals and Slots to signal changes of the accelerometer readings.

    The members x, y and z stores the previous values of the sensor reading in order to implement low pass filter to the values.

    In the implementation of the AccelerometerFilter class, we first read the value of the each axis from the QAccelerometerReading object. Then the values are converted from radians to degrees and applied the low pass filter to reduce the noise in the sensor readings. Different low pass factors are used depending on the platform (these were found out to be good by experimenting). Finally the calculated value is emitted.

    Note that the accelerometer sensors are oriented differently in Symbian and Maemo devices and we must differentiate this by using platform specific code.
    
    \snippet accelerometerfilter.cpp 0
    
    \section1 The Qt Quick UI
    
    The BubbleLevel.qml is the main QML element. It represents the wooden board of the bubble level and it also acts as connection point between QML and Qt side. In the beginning of the element there exists the two signals, two functions and one property. All of these define the interface between Qt and QML.
    
    The minimizeApplication is signalled when the application is wanted to be minimized on Maemo platform. The saveCorrectionAngle is signalled when a new calibration factor is required to be stored into the devices memory.
    
    The handleRotation function acts as Qt slot which the AccelerometerFilters signal rotationChanged is connected. Similarly the setCorrectionAngle function acts also as Qt slot to which the Settings objects signal correctionAngle is connected.
    
    The property alias taskSwitcherVisible is provided to allow the Qt model to show or hide the task switcher button which minimises the application. This is only meaningful in Maemo platforms, where every application normally has a task switcher button.
    
    \snippet qml/BubbleLevel.qml 0
    
    The Tube element represents the the glass tube of the bubble level. It is anchored to the center of the wooden board. The width and height are calculated with a specific factors to make the glass tube to scale to a different resolutions.
    
    \snippet qml/BubbleLevel.qml 1
    
    In the implementation of the Tube.qml the property deg represents the current inclination. The x-position of the bubble is binded to JavaScript function calX which is called every time when the property deg, center or bubblCenter is changed. The function places the bubble to the corresponding place on its parent.
    
    \snippet qml/Tube.qml 0
*/
